package com.jsbb.controller;

import com.jsbb.common.UserToken;
import com.jsbb.constant.BizConstant;
import com.jsbb.util.CookieUtils;
import com.jsbb.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**
 * IndexController
 *
 * @author w
 * @date 2021-03-05
 */
@Slf4j
@Controller
@ApiIgnore
public class IndexController {

    @GetMapping("/")
    public String login(HttpServletRequest request) {
        String token = CookieUtils.getCookie(request, BizConstant.COOKIE_NAME);
        if (token == null) {
            return "login";
        }
        UserToken userToken = (UserToken) RedisUtils.get(BizConstant.USER_TOKEN + token);
        if (userToken == null) {
            return "login";
        }
        return "home";
    }

    @GetMapping("/{var1}/{var2}/index")
    public String index(HttpServletRequest request, @PathVariable("var1") String var1, @PathVariable("var2") String var2) {
        String token = CookieUtils.getCookie(request, BizConstant.COOKIE_NAME);
        if (token == null) {
            return "login";
        }
        UserToken userToken = (UserToken) RedisUtils.get(BizConstant.USER_TOKEN + token);
        if (userToken == null) {
            return "login";
        }
        return var1 + "/" + var2;
    }
}
